from machine import Pin
import utime
import network
import ntptime 
from machine import RTC
import machine
rtc = RTC()
def sync_ntp():
    ntptime.NTP_DELTA = 3155644800   # 可选 UTC+8偏移时间（秒），不设置就是UTC0
    ntptime.host = 'ntp1.aliyun.com'  # 可选，ntp服务器，默认是"pool.ntp.org"
    try:
        ntptime.settime()   # 修改设备时间,到这就已经设置好了
    except:
        return False
    else:
        return True

sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.scan()       
#sta_if.connect("<AP_name>", "<password>") # Connect to an AP
print(sta_if.active())
def connectWifi():
    n=0
    if not sta_if.isconnected():
        sta_if.connect("Network_1", "15368274761")
        while not sta_if.isconnected():
            n=n+1
            print("connect...%02d"%(n))
            utime.sleep(1)
            if(n>=30):
                sta_if.active(False)
                return False

    #sync_ntp()



if __name__ == "__main__":
    connectWifi()
    while not sync_ntp() :
        t=rtc.datetime()
        print(t)
        utime.sleep_us(1000000-t[7])
        sync_ntp()
    IIC256.LCDInit()
    while True:
        t=rtc.datetime()
        print("{0:0>2d}:{1:0>2d}:{2:0>2d}".format(t[4],t[5],t[6]))
        print(t)
        utime.sleep_us(1000000-t[7])
        #machine.deepsleep((1000000-t[7])//1000)

    